#include "Daemon_FacInfoCtl.h"
#include "pox_net.h"
#include "Daemon_Common.h"

static INT NandRead(VOID *buf, INT buflen)
{
	FILE *Fp	= NULL;
	Fp	= fopen(FACINFONANDDEVPATH, "r");
	if (NULL == Fp){
		return -1;
	}
	
	if(0 > fread(buf, buflen, 1, Fp)){
		fclose(Fp);
		return -1;
	}
	fclose(Fp);
	return 0;
}
static INT NandErase()
{
	FILE *Fp	= NULL;
	char buf[4096];
	memset(buf, 0, sizeof(buf));
	Fp	= fopen(FACINFONANDDEVPATH, "w+");
	if (NULL == Fp){
		return -1;
	}
	
	if(0 > fwrite(buf, sizeof(buf), 1, Fp)){
		fclose(Fp);
		return -1;
	}
	fclose(Fp);
	return 0;
}

INT NandWrite(VOID *buf, INT buflen)
{
	FILE *Fp	= NULL;
	Fp	= fopen(FACINFONANDDEVPATH, "w+");
	if (NULL == Fp){
		return -1;
	}
	
	if(0 > fwrite(buf, buflen, 1, Fp)){
		fclose(Fp);
		return -1;
	}
	fclose(Fp);
	return 0;
}

INT CheckUuid(SAVENANDINFO_T *pInFacInfo)
{
	int i = 15;
	
	if(pInFacInfo == NULL)
		return -1;
	if(pInFacInfo->PTPUUID == NULL || strlen(pInFacInfo->PTPUUID) < 16 ||
		pInFacInfo->PTPUUID[0] == 0 || pInFacInfo->PTPUUID[0] == '\0')
		return -1;

	while(i >= 0){
		if(pInFacInfo->PTPUUID[i] >= '0' && pInFacInfo->PTPUUID[i] <= '9')
			i --;
		else
			return -1;
	}
	if( strlen(pInFacInfo->PTPUUID) > 16 )
		pInFacInfo->PTPUUID[16] = '\0';
		
	return 0;
}

#if 0

INT CheckCmei(SAVENANDINFO_T *pInFacInfo)
{
#if 0
	int i = 14;
	
	if(pInFacInfo == NULL)
		return -1;
	if(pInFacInfo->PTPCMEI== NULL || strlen(pInFacInfo->PTPCMEI) < 15 ||
		pInFacInfo->PTPCMEI[0] == 0 || pInFacInfo->PTPCMEI[0] == '\0')
		return -1;
	
	while(i >= 0){
		if(pInFacInfo->PTPCMEI[i] >= '0' && pInFacInfo->PTPCMEI[i] <= '9')
			i --;
		else
			return -1;
	}
	if( strlen(pInFacInfo->PTPCMEI) > 15 )
		pInFacInfo->PTPCMEI[15] = '\0';
#endif
	return 0;
}
#endif

INT IPCAM_ReadFacInfo(SAVENANDINFO_T *pFacInfo)
{
	int iRet = 0;
	char macAddr[24];	
	UCHAR MarkERR	= 0;
	SAVENANDINFO_T FacInfo;	
	
	memset (&FacInfo, 0, sizeof(FacInfo));
	if (-1 == NandRead(&FacInfo, sizeof(FacInfo))){
		IPCAM_DEBUG("Read Factory Info Err");
		MarkERR		= 1;
	}
	if (1	== MarkERR
		|| strlen((char*)FacInfo.devModel)		>= sizeof(FacInfo.devModel)
		|| strlen((char*)FacInfo.devSerialNumb)	>= sizeof(FacInfo.devSerialNumb)
		|| strlen((char*)FacInfo.devRegcode)	>= sizeof(FacInfo.devRegcode)
		|| strlen((char*)FacInfo.devName)		>= sizeof(FacInfo.devName)
		|| strlen((char*)FacInfo.szFactoryName)	>= sizeof(FacInfo.szFactoryName)
		|| strlen((char*)FacInfo.NetMacAddr)	>= sizeof(FacInfo.NetMacAddr)
		|| strlen((char*)FacInfo.PTPUUID)		>= sizeof(FacInfo.PTPUUID)
		/*|| strlen((char*)FacInfo.PTPDevKey)		>= sizeof(FacInfo.PTPDevKey)
		|| strlen((char*)FacInfo.PTPCMEI)		>= sizeof(FacInfo.PTPCMEI)*/
		|| strlen((char*)FacInfo.sensor)		>= sizeof(FacInfo.sensor)){

		if( 1 == ProductGetHandle()->bLAN )
		{
			pox_get_macaddr(NET_ETH, macAddr);
		}
		else 
		{
			pox_get_macaddr(NET_WLAN, macAddr);
		}
		
		sprintf((char*)FacInfo.devSerialNumb, "RL01-RL0808-%c%c%c%c%c%c%c%c%c%c%c%c123456", 
			macAddr[0], macAddr[1], macAddr[3], macAddr[4], macAddr[6], macAddr[7], macAddr[9], macAddr[10], 
			macAddr[12], macAddr[13], macAddr[15], macAddr[16]);
		strcpy  ((char*)FacInfo.NetMacAddr, 		macAddr);
		strcpy  ((char*)FacInfo.devModel, 			DEV_MODEL);
		strcpy  ((char*)FacInfo.devRegcode, 		DEV_REGCODE);
		strcpy  ((char*)FacInfo.devName,			DEV_SHOWNAME);
		strcpy  ((char*)FacInfo.szFactoryName, 		DEV_FACNAME);
		strcpy  ((char*)FacInfo.PTPUUID, 			DEFAULT_UID);
		strcpy  ((char*)FacInfo.PTPCMEI, 			DEFAULT_CMEI);
		strcpy  ((char*)FacInfo.PTPDevKey, 			DEFAULT_DEVKEY);
		strcpy  ((char*)FacInfo.sensor, 			DEFAULT_SENSOR);

		FacInfo.devNo		        =		DEV_NO;
		FacInfo.ThreeMachineSupport =       SUPPORT_THREEMACHINE;
		FacInfo.WifiOFF				=       SUPPORT_WIFI;
		FacInfo.SDOFF				=		SUPPORT_SD;
		FacInfo.AudioInOFF			=		SUPPORT_AUDIOINPUT;
		FacInfo.AudioOutOFF			=		SUPPORT_AUDIOOUTPUT;
		FacInfo.IOOFF				=		SUPPORT_IO;
		
		IPCAM_DEBUG("Read Factory flash Get a error msg so set default facinfo");
		iRet = FALSE;
	}else {
		IPCAM_DEBUG("Read Factory flash OK");
		iRet = TRUE;
	}
#ifdef __HuaweiLite__
    DevGetUuid(FacInfo.PTPUUID);
    DevConfigGetCmei(FacInfo.PTPCMEI, sizeof(FacInfo.PTPCMEI));
    DevConfigGetSecretKey(FacInfo.PTPDevKey, sizeof(FacInfo.PTPDevKey));
#endif /* __HuaweiLite__ */
	memcpy(pFacInfo, &FacInfo, sizeof(FacInfo));

	return iRet;
}

INT IPCAM_WriteFacInfo(SAVENANDINFO_T *pFacInfo)
{
	NandErase();
	if (-1 == NandWrite(pFacInfo, sizeof(SAVENANDINFO_T))){
		IPCAM_DEBUG("Write Factory Info Err");
		return -1;
	}
#ifdef __HuaweiLite__
    DevSetUuid(pFacInfo->PTPUUID);
    DevConfigSetCmei(pFacInfo->PTPCMEI);
    DevConfigSetSecretKey(pFacInfo->PTPDevKey);
#endif /* __HuaweiLite__ */

    return 0;
}


